#import "DDWebView.h"
#import <WebKit/WebKit.h>
@interface DDWebView()<WKNavigationDelegate>
{
    int _index;
}
@property (nonatomic, strong) WKWebView *webView;
@end
@implementation DDWebView
- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
- (WKWebView *)webView
{
    if (!_webView)
    {
        _webView = [[WKWebView alloc]init];
        _webView.navigationDelegate = self;
        [self addSubview:_webView];
        _webView.c_edges.c_equal(self);
    }
    return _webView;
}
- (void)dd_show:(NSString *)url
{
    NSString *new_url  = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:new_url]];
    request.HTTPMethod = @"GET";
    [CCHud startHud];
    [self.webView loadRequest:request];
    _index = 0;
}
- (void)dd_cleanCacheAndCookie{
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies]){
        [storage deleteCookie:cookie];
    }
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    NSURLCache * cache = [NSURLCache sharedURLCache];
    [cache removeAllCachedResponses];
    [cache setDiskCapacity:0];
    [cache setMemoryCapacity:0];
}
#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
    DeLog(@"页面开始加载时调用");
}
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
{
    DeLog(@"当内容开始返回时调用");
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    [CCHud stopHud];
    DeLog(@"页面加载完成之后调用");
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation
{
    [CCHud stopHud];
    DeLog(@"页面加载失败时调用");
}
- (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation
{
    [CCHud  startHud];
    DeLog(@"接收到服务器跳转请求之后调用");
}
- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error
{
    DeLog(@" 跳转失败时调用");
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
    WKNavigationActionPolicy policy = WKNavigationActionPolicyAllow;
    if ([[navigationAction.request.URL absoluteString] containsString:@"weixin://"] )
    {
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]])
        {
            [[UIApplication sharedApplication] openURL:navigationAction.request.URL];
        }
        else
        {
            [CCHud stopHud];
            [CCAlert showMessage:@"未安装微信,请先安装" title:@"注意" enter:nil];
        }
    }
    else if ([[navigationAction.request.URL absoluteString] containsString:@"mqq://"] )
    {
        [CCHud stopHud];
        [[UIApplication sharedApplication] openURL:navigationAction.request.URL];
    }
    decisionHandler(policy);
}
- (void)dealloc
{
    [self dd_cleanCacheAndCookie];
}
@end
